#include "EnemyController.h" 
#include "TollgateScene.h"


bool EnemyController::init(){
	m_moveSpeed = 5;
	m_rotaSpeed = 10;
	this->schedule(schedule_selector(EnemyController::changeDirection), 2.0f);
	this->schedule(schedule_selector(EnemyController::fire), 0.15f);
	this->scheduleUpdate();
	return true;
}

void EnemyController::stop(){
	this->unscheduleUpdate();
	this->unschedule(schedule_selector(EnemyController::changeDirection));
	this->unschedule(schedule_selector(EnemyController::fire));
	//_eventDispatcher->removeAllEventListeners();
}

void EnemyController::update(float dt){
	auto m_player = Player::create(Node::create());
	
	auto pos = m_controllerListener->getThis()->convertToWorldSpace(Vec2(0, 0));
	auto ePos = m_player->convertToWorldSpace(Vec2(0, 0));
	auto l = sqrt(pow(ePos.x - pos.x, 2) + pow(ePos.y - pos.y, 2));
	float rotation = 0;
	if (l != 0){
		if (ePos.x - pos.x >= 0 && ePos.y - pos.y >= 0){
			rotation = 90 - CC_RADIANS_TO_DEGREES(asin(abs((ePos.y - pos.y) / l)));
		}
		if (ePos.x - pos.x >= 0 && ePos.y - pos.y < 0){
			rotation = 90 + CC_RADIANS_TO_DEGREES(asin(abs((ePos.y - pos.y) / l)));
		}
		if (ePos.x - pos.x < 0 && ePos.y - pos.y < 0){
			rotation = 270 - CC_RADIANS_TO_DEGREES(asin(abs((ePos.y - pos.y) / l)));
		}
		if (ePos.x - pos.x < 0 && ePos.y - pos.y >= 0){
			rotation = 270 + CC_RADIANS_TO_DEGREES(asin(abs((ePos.y - pos.y) / l)));
		}
	}
	m_controllerListener->setTankHead(rotation);
	 pos = m_controllerListener->getTagPosition();
	float rota = m_controllerListener->getTagRotation();
	pos += Vec2(m_moveSpeed*sin(CC_DEGREES_TO_RADIANS(rota)), m_moveSpeed*cos(CC_DEGREES_TO_RADIANS(rota)));
	if (!m_controllerListener->setTagPosition(pos.x, pos.y)){
		m_stop = true;
	}
	else{
		m_stop = false;
	}

	if (rota < m_direction){
		if (abs(m_direction - rota) <= abs(rota + 360 - m_direction)){
			m_controllerListener->setTagRotation(rota + m_rotaSpeed);
		}
		else{
			if (rota - m_rotaSpeed < 0){
				m_controllerListener->setTagRotation(360 + rota - m_rotaSpeed);
			}
			else{
				m_controllerListener->setTagRotation(rota - m_rotaSpeed);
			}
		}
	}
	else if (rota > m_direction){
		if (abs(rota - m_direction) <= abs(360 - rota + m_direction)){
			m_controllerListener->setTagRotation(rota - m_rotaSpeed);
		}
		else{
			if (rota + m_rotaSpeed >= 360){
				m_controllerListener->setTagRotation(rota + m_rotaSpeed - 360);
			}
			else{
				m_controllerListener->setTagRotation(rota + m_rotaSpeed);
			}
		}
	}
	if (abs(rota - m_direction) < m_rotaSpeed){
		m_controllerListener->setTagRotation(m_direction);
	}
}

void EnemyController::changeDirection(float dt){
	if (!m_stop) return;
	m_direction = rand_0_1() * 360;
}

void EnemyController::fire(float dt){
	((TollgateScene*)(m_controllerListener->get_Parent()->getParent()->getParent()))->addBullet((Entity*)(m_controllerListener->getThis()), m_controllerListener->getTagPosition(), false, ((Entity*)(m_controllerListener->getThis()))->getAtk(), ((Entity*)(m_controllerListener->getThis()))->getBullet());
}